package com.weiyv.Updater;

import android.content.Context;
import android.util.Log;
import com.weiyv.Updater.utils.AndroidTool;
import com.weiyv.Updater.utils.MiscFunc;
import com.weiyv.Updater.utils.NetTool;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WeiyvClient {
    private static WeiyvClient instance = new WeiyvClient();
    private Context context;
    private String instructionURL = "http://tvbox.13975808456.com/tvBox/Instruction/getInstruction";
    private String reportApkURL = "http://tvbox.13975808456.com/tvBox/tvBoxSite/tvBox/reportApk";

    private WeiyvClient() {
    }

    public static WeiyvClient getInstance() {
        return instance;
    }

    public void init(Context ctx) {
        context = ctx;
    }

    public void reportApk() {
        HttpEntityEnclosingRequestBase httpRequest = new HttpPost(reportApkURL);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ID", AndroidTool.getInstance().getDeviceID()));
        params.add(new BasicNameValuePair("apks", AndroidTool.getInstance().listApplication()));
        try {
            httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            new DefaultHttpClient().execute(httpRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public HashMap getInstruction() {
        String url = instructionURL + "?ID=" + AndroidTool.getInstance().getDeviceID() + NetTool.getInstance().getLocation();
        String res = NetTool.getInstance().getURL(url);
        //Toast.makeText(context,res,Toast.LENGTH_SHORT).show();
        return MiscFunc.Json2HashMap(res, new String[]{
                "InstallApk", "UninstallApk", "RunApp", "ReportApk", "AccessWebPage", "UpdateIndex", "Notify", "LockDown"});
    }
}
